//  Created by EasyIOS on 14-4-10.
//  Copyright (c) 2014年 zhuchao. All rights reserved.
//

#import "NSDate+XY.h"


@implementation NSDate (XY)

- (NSInteger)year {
	return [[NSCalendar currentCalendar] components:NSCalendarUnitYear fromDate:self].year;
}

- (NSInteger)month {
	return [[NSCalendar currentCalendar] components:NSCalendarUnitMonth fromDate:self].month;
}

- (NSInteger)day {
	return [[NSCalendar currentCalendar] components:NSCalendarUnitDay fromDate:self].day;
}

- (NSInteger)hour {
	return [[NSCalendar currentCalendar] components:NSCalendarUnitHour fromDate:self].hour;
}

- (NSInteger)minute {
	return [[NSCalendar currentCalendar] components:NSCalendarUnitMinute fromDate:self].minute;
}

- (NSInteger)second {
	return [[NSCalendar currentCalendar] components:NSCalendarUnitSecond fromDate:self].second;
}

- (NSInteger)weekday {
	return [[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:self].weekday;
}

- (NSString *)stringWithDateFormat:(NSString *)dateFormat {
	NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
	formatter.dateFormat = dateFormat;
	return [formatter stringFromDate:self];
}

- (NSString *)timeLeft {
	long int delta = lround( [self timeIntervalSinceDate:[NSDate date]] );

    NSMutableString * result = [NSMutableString string];
    
    if ( delta >= YEAR ) {
		NSInteger years = ( delta / YEAR );
        [result appendFormat:@"%ld年", (long)years];
        delta -= years * YEAR ;
    }
    
	if ( delta >= MONTH ) {
        NSInteger months = ( delta / MONTH );
        [result appendFormat:@"%ld月", (long)months];
        delta -= months * MONTH ;
	}
    
    if ( delta >= DAY ) {
        NSInteger days = ( delta / DAY );
        [result appendFormat:@"%ld天", (long)days];
        delta -= days * DAY ;
    }
    
    if ( delta >= HOUR ) {
        NSInteger hours = ( delta / HOUR );
        [result appendFormat:@"%ld小时", (long)hours];
        delta -= hours * HOUR ;
    }
    
    if ( delta >= MINUTE ) {
        NSInteger minutes = ( delta / MINUTE );
        [result appendFormat:@"%ld分钟", (long)minutes];
        delta -= minutes * MINUTE ;
    }

	NSInteger seconds = ( delta / SECOND );
	[result appendFormat:@"%ld秒", (long)seconds];

	return result;
}

+ (NSDate *)dateWithString:(NSString *)string dateFormat:(NSString *)dateFormat {
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = dateFormat;
    return [formatter dateFromString:string];
}

+ (NSString *)stringWithDateFormat:(NSString *)dateFormat secs:(NSTimeInterval)secs {
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = dateFormat;
    return [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:secs]];
}

+ (NSString *)descriptionWithDateFormat:(NSString *)dateFormat secs:(NSTimeInterval)secs {
    NSTimeInterval temp = [[self date] timeIntervalSince1970] - secs;
    if (temp < 60) {
        return [NSString stringWithFormat:@"刚刚"];
    }
    else if ((temp = temp / 60) < 60) {
        return [NSString stringWithFormat:@"%.0f分钟前", temp];
    }
    else if ((temp = temp / 60) < 24) {
        return [NSString stringWithFormat:@"%.0f小时前", temp];
    }
    else {
        return [self stringWithDateFormat:dateFormat secs:secs];
    }
}

@end
